package com.zxy.supplier_system.server.entity

import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.OffsetDateTime

/**
 * 盘点
 */
@Entity
@EntityListeners(AuditingEntityListener::class)
@DynamicUpdate
@DynamicInsert
class MarketCheckInventoryRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id = 0

    @CreatedDate
    lateinit var createdDateTime:OffsetDateTime

    lateinit var name:String

    @CreatedBy
    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var createdBy: Employee

    var submittedDateTime: OffsetDateTime? = null

    @OneToMany(mappedBy = "record", orphanRemoval = true, cascade = [CascadeType.ALL])
    @OrderBy(value = "sequence asc")
    var items:MutableList<MarketCheckInventoryRecordItem> = mutableListOf()

    @ManyToOne
    @JoinColumn(nullable = false)
    lateinit var market: Market

}

val List<MarketCheckInventoryRecordItem>.nextSequence: Int
    get() = this.maxOfOrNull {
        it.sequence
    }?.plus(1)?:0